package vn.gq.cd.view;

import vn.gq.app4vn.doctruyen.Configuration;
import vn.gq.app4vn.doctruyen.OnStartActivityCallBack;
import vn.gq.app4vn.doctruyen.R;
import vn.gq.app4vn.doctruyen.ReaderActivity;
import vn.gq.app4vn.doctruyen.UngDungVietApp;
import vn.gq.cd.model.ChapterAdapter;
import vn.gq.cd.model.bean.CategoryItem;
import vn.gq.cd.widget.BaseFragment;
import vn.gq.tym.utils.IClientListener;
import vn.gq.tym.utils.OnNetworkListener;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

public class ChapterFragment extends BaseFragment implements OnItemClickListener, OnNetworkListener, OnClickListener {

	private ListView mListView;
	private ProgressBar mProgressBar;
	private ChapterAdapter mAdapter;
	private CategoryItem mCategoryItem;
	private View mBounder;
	private Button mBtnRetry;
	private int selectedItemPos = -1;
	private boolean readingFirstTime = true;
	
	public static ChapterFragment newInstance(CategoryItem catItem, int selectedPos) {
		ChapterFragment f = new ChapterFragment();
		Bundle args = new Bundle();
		args.putParcelable("CATEGORY", catItem);
		args.putInt("SELECTED_POS", selectedPos);
		f.setArguments(args);
		return f;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			mCategoryItem = getArguments().getParcelable("CATEGORY");
			selectedItemPos = getArguments().getInt("SELECTED_POS");
		}
		setRetainInstance(true);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (!mCategoryItem.getName().equals(""))
			getSherlockActivity().getSupportActionBar().setSubtitle(mCategoryItem.getName());
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if (mAdapter != null) {
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public int getLayoutId() {
		return R.layout.fragment_chapter;
	}

	@Override
	public int getContainerId() {
		return R.id.layout_container;
	}

	@Override
	public void createView(Bundle savedInstanceState) {
		mListView = (ListView) findViewById(R.id.list);
		mListView.setOnItemClickListener(this);
		mBounder = findViewById(R.id.nonetwork);
		mBtnRetry = (Button) findViewById(R.id.btn_reconnect);
		mBtnRetry.setOnClickListener(this);
		mProgressBar = (ProgressBar) findViewById(R.id.loading);
		mAdapter = new ChapterAdapter(getActivity(), this);
		if (selectedItemPos > -1) {
			mAdapter.setSelectedPos(selectedItemPos);
		}
		load(false);
	}

	
	public void load(final boolean reload) {
		mProgressBar.setVisibility(View.VISIBLE);
		mAdapter.load(mCategoryItem.getPath(), new Runnable() {
			
			@Override
			public void run() {
				mListView.setAdapter(mAdapter);
				mProgressBar.setVisibility(View.GONE);
				if (getActivity() instanceof IClientListener && !Configuration.MULTI_STORIES_APP) {
					((IClientListener) getActivity()).onDone(0, null);
				}
			}
		});
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (id > -1) {
			ReaderActivity.startActivity(getActivity(), mCategoryItem, mAdapter.getData(), position, 0, readingFirstTime, new OnStartActivityCallBack() {
				
				@Override
				public void onDone(Intent intent) {
					startActivityForResult(intent, 1);
				}
			});
			mAdapter.setSelectedPos(position);
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		UngDungVietApp application = (UngDungVietApp) getActivity().getApplication();
		++application.backCountTimes;
		if (resultCode == RESULT_OK) {
			int position = data.getExtras().getInt("position");
			boolean visted = data.getExtras().getBoolean("visited");
			if (position >= 0) {
				mAdapter.setSelectedPos(position);
			}
			if (visted == true) {
				readingFirstTime = false;
			}
		}
	}

	@Override
	public void onClick(View v) {
		mBounder.setVisibility(View.GONE);
		load(true);
	}

	@Override
	public void onNoNetwork() {
		mBounder.setVisibility(View.VISIBLE);
	}
	
	public void setLastShowIMAd(Long t) {
		try {
			SharedPreferences pref = getActivity().getSharedPreferences(getClass().getSimpleName(), Context.MODE_PRIVATE);
			pref.edit().putLong("last_show_imad", t).commit();
		
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public long getLastShowIMAd() {
		SharedPreferences pref = getActivity().getSharedPreferences(getClass().getSimpleName(), Context.MODE_PRIVATE);
		Long t = pref.getLong("last_show_imad", 0);
		return t;
	}
	
}
